﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WD.Db.BLL;
using WD.Db.Contract;
using WD.Db.Contract.Model;

namespace WD.Web.MVC.Common
{
    public static class StaticData
    {
        public static string lockFenRun计算 = "lockFenRun计算";
        /// <summary>
        /// 提现申请，避免并发问题重复提现，lock变量
        /// </summary>
        public static string lockTiXianYuE = "lockTiXianYuE";
        /// <summary>
        /// 管理员审核提现申请，避免并发问题重复提现，lock变量
        /// </summary>
        public static string lockTiXianSH = "lockTiXianSH";
        /// <summary>
        /// 自营店铺编号
        /// </summary>
        public const int ZyShopId = 1;

        /// <summary>
        /// 默认的对账的商家ID
        /// </summary>
        public static int defaultSelleId = -1;

        /// <summary>
        /// 微信OpenID
        /// </summary>
        public static string OpenID = "";


        /// <summary>
        /// 全国省级
        /// </summary>
        public static readonly List<string> ProList = new List<string>()
        {
            "北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "内蒙古", 
            "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省", "福建省",
            "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", 
            "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", 
            "新疆", "香港", "澳门", "台湾省"
        };

        ///// <summary>
        ///// 获取当前平台设置（仅限前台使用）
        ///// </summary>
        //public static BasicSetting CurrentSetting
        //{
        //    get
        //    {
        //        if (HttpContext.Current.Session["CurrentSetting"] != null)
        //        {
        //            return (BasicSetting)HttpContext.Current.Session["CurrentSetting"];
        //        }
        //        return null;
        //    }
        //    set { HttpContext.Current.Session["CurrentSetting"] = value; }
        //}

        /// <summary>
        /// 获取当前前台用户（仅限前台使用）
        /// </summary>
        public static User CurrentUser
        {
            get
            {
                if (HttpContext.Current.Request.Cookies["User"] == null)
                {
                    return null;
                }
                var uid = HttpContext.Current.Request.Cookies["User"].Value;
                DateTime ds = HttpContext.Current.Request.Cookies["User"].Expires;
                //if (ds < DateTime.Now)
                //{
                //    return null;
                //}
                if (!string.IsNullOrWhiteSpace(uid))
                {
                    if (HttpContext.Current.Session["User"] != null)
                    {
                        return (User)HttpContext.Current.Session["User"];
                    }
                    else
                    {
                        UserService u = new UserService();
                        var request = new UserRequest();
                        request.Include = new List<string> {
                            "AgentManager"
                        };
                        request.ID = Convert.ToInt32(uid);
                        var model = u.GetUser(request);
                        HttpContext.Current.Session["User"] = model;
                        return model;
                    }

                }
                else
                {
#warning 方便测试，自动登录
                    //UserService u = new UserService();
                    //CurrentUser = u.GetUser(1);
                    //return CurrentUser;
                    return null;
                }
            }
            set
            {
                HttpContext.Current.Session["User"] = value;
                if (value == null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies["User"];
                    if (cookie == null) return;
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    HttpContext.Current.Request.Cookies.Remove("User");
                    return;
                }
                HttpCookie cookieINS = new HttpCookie("User");
                cookieINS.Expires = DateTime.Now.AddDays(15);
                cookieINS.Value = value.ID.ToString();
                HttpContext.Current.Response.Cookies.Add(cookieINS);
            }
        }

    }
}